<?php
require '../vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$es = ClientBuilder::create()->setHosts(array('127.0.0.1:9200'))->build();
$keyword = $_GET['keyword'];

/*$json = '{
  "size": 0,
  "suggest": {
    "article-suggester": {
      "prefix": "",
      "completion": {
        "field": "title_completion"
      }
    }
  }
}';*/

$params = [
    'index' => 'titles',
    'body' => [
        'suggest' => [
            'article-suggester' => [
                'prefix' => $keyword,
                'completion' => [
                    'field' => 'title_completion'
                ],
            ],
        ],
    ],
    'size' => 0,
];
//$json = json_decode($json,true);

$res = $es->search($params);
if ($res) {
    $suggest = array_column(array_column($res['suggest']['article-suggester'][0]['options'], '_source'),'title_completion');
    $data = [
        'status' => 0,
        'data' => $suggest,
        'msg' => '获取成功'
    ];
    $data = json_encode($data, JSON_UNESCAPED_UNICODE);
    echo $data;
}

/*//创建文档 -- 相当于关系型数据库中的数据库
$params = array(
    'index' => 'dq_1',      //文档名称
    'body' => [
        'settings' => [
            'number_of_shards' => 1,       //设置主分片
            'number_of_replicas' => 1,      //主分片副本数
        ],
        'mappings' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'name' => [
                    'type' => 'text',
                    'index' => true
                ],
                'age' => [
                    'type' => 'integer'
                ],
                'sex' => [
                    'type' => 'keyword',
                    'index' => false
                ],
            ],
        ],
    ],
);

$res = $es->indices()->create($params);
echo $res ? '创建成功' : '失败';*/

/*//添加索引数据
$params = [
    'index' => 'dq_1',
    'id' => 'my_id',
    'body' => array('name' => 'abc', 'age' => 42, 'sex' => '男')
];
$res = $es->index($params);
echo $res ? 'success' : 'failed';*/

/*//查询数据
$params = [
    'index' => 'dq_1',
    'body' => [
        "query" => [
            "match" => [
                'name' => 'abc',
            ],
        ],
        'highlight' => [
            'fields' => [
                'name' => new stdClass()
            ],
        ],
    ],
];

$res = $es->search($params);
echo "<pre>";
print_r($res);
echo "<pre>";*/

/*//批量插入
$params = [];
for ($i=1; $i<40; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'dq_1',
            '_id' => $i,
        ],
    ];
    $params['body'][] = [
        'name' => '编程' . $i,
        'sex' => '男',
        'age' => (20 + $i)
    ];
}
echo "<pre>";
print_r($params);
echo "<pre>";

$res = $es->bulk($params);
echo $res ? '批量success' : 'false';*/


/*$params = [
    'index' => 'dq_1',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    'match' => [
                        'name' => '编程'
                    ],
                ],
                'filter' => [
                    'range' => [
                        "age" => [
                            'gt' => 20,
                            'lt' => 30,
                        ],
                    ],
                ],
            ],
        ],
        "size" => 5,
        'sort' => [
            ['age' => ['order' => 'desc']]
        ],
    ],
];

$res = $es->search($params);
echo "<pre>";
print_r($res);
echo "<pre>";*/


/*$params = [
    'index' => 'dq_1',
    'id' => 1
];

$res = $es->get($params);
echo "<pre>";
print_r($res);
echo "<pre>";*/

/*$params = [
    'index' => 'dq_1',
    'body' => ['ids' => [1,2,3,4]],
];*/

/*
 * 批量获取索引
 * $params = [
    'index' => 'dq_1',
    'body'  => [
        'ids' => [1, 2, 3, 4],
    ],
];

$res = $es->mget($params);
echo "<pre>";
print_r($res);
echo "<pre>";*/

/*//获取单个索引
$params = [
    'index' => 'dq_1',
    '_source' => ['name'],
    'id' => 1
];
$res = $es->get($params);
echo "<pre>";
print_r($res);
echo "<pre>";*/





